package com.wanmait.zaisousuo.webcontroller;

import com.github.pagehelper.PageInfo;
import com.wanmait.zaisousuo.pojo.Question;
import com.wanmait.zaisousuo.pojo.Userinfo;
import com.wanmait.zaisousuo.service.GarbageTypeService;
import com.wanmait.zaisousuo.service.QuestionService;
import com.wanmait.zaisousuo.service.TextureService;
import com.wanmait.zaisousuo.util.Pager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/web/question")
public class QuestionWebController {
    @Resource
    private QuestionService questionService;
    @Resource
    private GarbageTypeService garbageTypeService;
    @Resource
    private TextureService textureService;
//    次级列表以li标签显示
    @RequestMapping("nextTextureDataAsList")
    public ModelAndView nextTextureDataAsList(Integer textureId){
        ModelAndView mv = new ModelAndView();
        mv.addObject("textures",textureService.findChildrenTexture(textureId));
        return mv;
    }

//    次级材质以下拉列表框显示
    @RequestMapping("nextTextureData")
    public ModelAndView nextTextureData(int textureId){
        ModelAndView mv = new ModelAndView();
        mv.addObject("textures",textureService.findChildrenTexture(textureId));
        return mv;
    }

//    添加问题
    @RequestMapping("insert")
    public String insert(Question question,HttpServletRequest request){
        Userinfo userinfo = (Userinfo) request.getSession().getAttribute("userinfo");
        question.setUserinfo(userinfo);
        questionService.insert(question);
        return "redirect:/web/question/list";
    }
//    跳转到添加问题页面
    @GetMapping("add")
    public ModelAndView add(){
        ModelAndView mv = new ModelAndView();
        mv.addObject("garbageTypes",garbageTypeService.findAllType());
        mv.addObject("textures",textureService.findAll());
        return mv;
    }

//    跳转到展示页面，并带上垃圾种类以及材质
    @RequestMapping("list")
    public ModelAndView toArticleListPage(){

        ModelAndView mv = new ModelAndView();
        mv.addObject("garbageTypes",garbageTypeService.findAllType());
        mv.addObject("textures",textureService.findAll());
        return mv;
    }

//    垃圾数据
    @RequestMapping("listData")
    public void list(Integer pageNum, Integer garbageTypeId, Integer textureId, Model model){
        if (pageNum==null) pageNum=1;
        PageInfo<Question> pageInfo = questionService.findList(pageNum,garbageTypeId,textureId);
        int[] pages = pageInfo.getNavigatepageNums();
        model.addAttribute("pageInfo",pageInfo);
    }
    @RequestMapping("searchList")
    public void list(String keyword){

    }

    @RequestMapping("searchData")
    @ResponseBody
    public ModelAndView list(String keyword,Integer pageNum)
    {
        if(pageNum==null) pageNum=1;

        ModelAndView mv = new ModelAndView();
//        获取数据条数
        Integer dateCount=questionService.getDateCount(keyword,pageNum);
        Pager pager = new Pager(5,dateCount,pageNum);
        List<Question> questions= questionService.findQuestion(keyword,pageNum,pager);

        mv.addObject("pager",pager);
        mv.addObject("questions",questions);
        return mv;
    }

//    展示
    @RequestMapping("show")
    public ModelAndView show(Integer questionId,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        Question question = questionService.findById(questionId);
        mv.addObject("question",question);
        return mv;
    }
}
